package org.example.demo.service.userService.impl;

import org.example.demo.dto.ProductDetail.*;
import org.example.demo.service.userService.CommentService;
import org.example.demo.service.userService.ProductService;
import org.example.mbg.mapper.ProductMapper;
import org.example.mbg.mapper.ShopMapper;
import org.example.mbg.mapper.UserMapper;
import org.example.mbg.model.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 用户查看商品详情service
 */
@Service
public class ProductServiceImpl implements ProductService {
    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private CommentService commentService;

    public ProductDetailParam detail(Long id) {
        ProductDetailParam res = null;
        CommentExample commentExample;
        Product p = productMapper.selectByPrimaryKey(id);
        if (p != null) {
            Shop shop = shopMapper.selectByPrimaryKey(p.getShopId());
            if (shop != null) {

                commentExample = new CommentExample();
                commentExample.createCriteria().andProductIdEqualTo(p.getId());
                List<Comment> commentList = commentService.getCommentOfProduct(id);
                List<CommentInfoParam> commentInfoParamList = commentList.stream().map(e -> {
                    CommentInfoParam commentInfoParam = new CommentInfoParam();
                    User user = userMapper.selectByPrimaryKey(e.getUserId());
                    List<String> imgList = new ArrayList<>();

                    commentInfoParam.setUname(user.getUsername());
                    commentInfoParam.setAvatar(user.getAvatar());
                    commentInfoParam.setContent(e.getContent());
                    commentInfoParam.setCreated(e.getCreateTime());
                    commentInfoParam.setStyle(e.getStyle());
                    imgList.add(e.getImg());
                    commentInfoParam.setImages(imgList);
                    return commentInfoParam;

                }).collect(Collectors.toList());

                ShopInfoParam shopInfoParam = new ShopInfoParam();
                shopInfoParam.setId(shop.getId());
                shopInfoParam.setLogo(shop.getLogo());
                shopInfoParam.setName(shop.getName());
                shopInfoParam.setGoodsCount(shop.getPcounts());
                shopInfoParam.setSells(shop.getSells());

                res = new ProductDetailParam();
                List<String> service = new ArrayList<>();
                service.add("不好吃不要钱");
                service.add("中通快递");
                service.add("次日达");
                res.setIid(p.getId());
                res.setOldPrice(p.getDiscount().add(new BigDecimal(5)));
                res.setNewPrice(p.getDiscount());
                res.setDesc(p.getDescription());
                res.setServices(service);
                res.setTitle(p.getName());
                List<String> picList = Arrays.asList(p.getAlbumPics().split(","));
                res.setDetailImages(picList);
                res.setTopImages(picList);

                res.setRecommends(getRecommend(id));
                res.setDetailInfo(getProductDesc(id));

                res.setShopInfo(shopInfoParam);
                res.setCommentInfo(commentInfoParamList);
            }

        }
        return res;
    }

    @Override
    public int addFavourite(Long id) {
        Product p = productMapper.selectByPrimaryKey(id);
        if (p != null) {
            p.setCfav(p.getCfav() + 1);
            return productMapper.updateByPrimaryKeySelective(p);
        }
        return 0;
    }

    @Override
    public int cancelFavourite(Long id) {
        Product p = productMapper.selectByPrimaryKey(id);
        if (p != null) {
            long cfav = p.getCfav() - 1L;
            p.setCfav(cfav > 0 ? cfav : 0);
            return productMapper.updateByPrimaryKeySelective(p);
        }
        return 0;
    }

    @Override
    public List<ProductInfoSimple> getRecommend(Long id) {
        ProductExample productExample = new ProductExample();
        return productMapper.selectByExample(productExample).subList(0, 20).stream().filter(e -> !e.getId().equals(id)).map(e -> {
            ProductInfoSimple productInfoSimple = new ProductInfoSimple();
            productInfoSimple.setId(e.getId());
            productInfoSimple.setCfav(e.getCfav());
            productInfoSimple.setImg(Arrays.asList(e.getAlbumPics().split(",")).get(0));
            productInfoSimple.setTitle(e.getName());
            productInfoSimple.setPrice(e.getPrice());
            return productInfoSimple;
        }).collect(Collectors.toList());
    }

    @Override
    public List<ProdectDesc> getProductDesc(Long id) {
        Product product = productMapper.selectByPrimaryKey(id);
        List<ProdectDesc> list = new ArrayList<>();
        if (product != null) {
            ProdectDesc prodectDesc = new ProdectDesc();
            prodectDesc.setKey(product.getDetailKey());
            prodectDesc.setList(Arrays.asList(product.getDetailImg().split(",")));
            list.add(prodectDesc);
        }
        return list;
    }
}
